@extends('Layout/base')
@section('title','注册')



@section('main')
    <div class="container">
        
        <div class="form-group">
            用户名：<input type="text" name="uname" class="form-control">
        </div>
        <div class="form-group">
            密码：<input type="text" name="upass" class="form-control">
        </div>

      <div class="form-group">
          <img src="{{url('/makecode')}}" alt="">
          <input type="text" name="code" />
      </div>
      
        <div class="form-group">
            <a href="javascript:;" class="btn btn-danger btn-register">注册</a>
        </div>
    </div>

@endsection

@section('footer-js')

    <script type="text/javascript">

        $('a.btn-register').on('click',function(){

        	var uname = $('input[name="uname"]').val();

        	var upass = $('input[name="upass"]').val();

            var ucode = $('input[name="code"]').val();



        	$.ajax({
        		type:'post',
        		url:'{{url("/doregister")}}',
        		data:'name='+uname+'&pass='+upass+'&_token={{csrf_token()}}'+'&code='+ucode,
        		success:function(data){

        			//用户名不存在
        			if(data.status == 1404) {
        				var tip = '<div class="alert alert-danger alter-register-tip">'+data.msg+'</div>';
        			}else if(data.status == 1200) {

        				var tip = '<div class="alert alert-success alter-register-tip">'+data.msg+'</div>';

        				 location.href = '{{url("/login")}}';

        			}else if(data.status == 1402){

        				 var tip = '<div class="alert alert-danger alter-register-tip">'+data.msg+'</div>';
        			}else if(data.status == 1111){

                         var tip = '<div class="alert alert-danger alter-register-tip">'+data.msg+'</div>';
                    }

        			setTimeout(function (){$('.alter-register-tip').remove()},3000);
        			$('.container').prepend(tip);
        		},

        		error:function(tipString) {

        			if(tipString.responseText) {
        				var tipDiv = `<div class="alert alert-danger alter-tip">
        				<ul>`;

        				var tipObj = JSON.parse(tipString.responseText).errors;

        				for(var key in tipObj){

        					tipDiv +='<li>'+tipObj[key][0]+'</li>';
        				}

        				tipDiv +='</ul></div>';

        				$('.alter-tip').remove();
        				$('.container').prepend(tipDiv);
        			}
        		},

        		dataType:'json'
        	});
        });
    </script>

@endsection